<?php
/**
 * Created by PhpStorm.
 * User: BesonTD
 * Date: 15-2-28
 * Time: 下午3:42
 */
//引入短信类
if (!defined('APP_IN')) exit('Access Denied');
//include ('page.php');
include(INC_DIR . 'api.func.php');

//引入短信类
include(INC_DIR . 'Sms_api.php');
//允许操作
$ac_arr = array(
    'register' => '短信操作',
    'pwd' => '忘记密码操作',
);
//当前操作
$ac = isset($_REQUEST['a']) && isset($ac_arr[$_REQUEST['a']]) ? $_REQUEST['a'] : '';
if (!$ac) return false;
//注册
if ($ac == 'register') {
    $mobile = isset($_POST['mobile']) && !empty($_POST['mobile']) ? $_POST['mobile'] : '';
    $type = isset($_POST['type']) && !empty($_POST['type']) ? $_POST['type'] : 1;

    if (empty($mobile) || !preg_match('/^1[3-9]\d{9}$/', $mobile)) {
        echo json_encode(array('succ' => 'false', 'msg' => '手机号为空或格式错误'));
        exit;
    }
    $sms_status = sms_send_status($mobile, $type);
    if (is_array($sms_status)) {
        echo json_encode($sms_status);
        exit;
    }
    $code = get_code(6);
    if (sendSMS($mobile, $code)) {
        $time = time();
        $db->row_insert('sms_records', array('mobile' => $mobile, 'code' => $code . '_' . $time, 'type' => $type,'dateline'=>$time));
        echo json_encode(array('succ' => 'true', 'msg' => '发送成功！'));
        exit;
    }

}
if ($ac == 'pwd') {
    $mobile = isset($_POST['mobile']) && !empty($_POST['mobile']) ? $_POST['mobile'] : '';
    $type = isset($_POST['type']) && !empty($_POST['type']) ? $_POST['type'] : 2;

    if (empty($mobile) || !preg_match('/^1[3-9]\d{9}$/', $mobile)) {
        echo json_encode(array('succ' => 'false', 'msg' => '手机号为空或格式错误'));
        exit;
    }
    $sms_status = sms_send_status($mobile, $type);
    if (is_array($sms_status)) {
        echo json_encode($sms_status);
        exit;
    }
    $code = get_code(6);

    if (sendSMS($mobile, $code)) {
        $time = time();
        $db->row_insert('sms_records', array('mobile' => $mobile, 'code' => $code . '_' . $time, 'type' => $type,'dateline'=>$time));
        echo json_encode(array('succ' => 'true', 'msg' => '发送成功！'));
        exit;
    }
}
